/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.feature.tidb;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import yhao.infra.feature.tidb.mybatis.dialect.TidbDialect;
import yhao.infra.service.YhaoInfraServiceProperties;

import java.util.concurrent.ScheduledExecutorService;

/**
 * tidb配置类
 *
 * @author yoara
 */
@ComponentScan("yhao.infra.feature.tidb.mybatis")
@EnableConfigurationProperties(YhaoTidbProperties.class)
public class TidbConfig {
    @Autowired
    private Environment env;

    @Bean(name="tidbCreateScheduler")
    public ScheduledExecutorFactoryBean tidbCreateScheduler(YhaoTidbProperties properties){
        ScheduledExecutorFactoryBean scheduledExecutorFactoryBean = new ScheduledExecutorFactoryBean();
        scheduledExecutorFactoryBean.setPoolSize(properties.getDruidScheduledExecutorPoolSize());
        return scheduledExecutorFactoryBean;
    }

    @Bean(name="tidbDefaultDataSource",initMethod = "init",destroyMethod = "close")
    public DruidDataSource tidbDefaultDataSource(YhaoTidbProperties properties,
            ScheduledExecutorService tidbCreateScheduler){
        return ConfigFactory.makeTidbDruidDataSource(properties, tidbCreateScheduler);
    }

    @Bean(name="tidbDataSource")
    public DruidDataSource tidbDataSource(DruidDataSource tidbDefaultDataSource){
        DruidDataSource defaultDataSource = tidbDefaultDataSource;
        //多库切换
        //DynamicDataSource source = new DynamicDataSource();
        //Map<Object, Object> targetDataSources =
        //        ImmutableMap.of("defaultDataSource",defaultDataSource);
        //source.setTargetDataSources(targetDataSources);
        //source.setDefaultTargetDataSource(defaultDataSource);

        return defaultDataSource;
    }

    @Bean(name="tidbSqlSessionFactory")
    public SqlSessionFactory tidbSqlSessionFactory(
            DruidDataSource tidbDataSource, YhaoInfraServiceProperties properties) throws Exception {
        SqlSessionFactory factory = ConfigFactory.makeSqlSessionFactory(properties,tidbDataSource, TidbDialect.class);
        return factory;
    }

    @Bean(name="tidbJdbcTemplate")
    public JdbcTemplate tidbJdbcTemplate(DruidDataSource tidbDataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(tidbDataSource);
        return jdbcTemplate;
    }

    @Bean(name = "tidbTxManager")
    public PlatformTransactionManager tidbTxManager(DruidDataSource tidbDataSource) {
        return new DataSourceTransactionManager(tidbDataSource);
    }
}
